﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL.ReportBL;

public partial class Report_IntergralWasteBook : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("900500");
        if (!IsPostBack)
        {
            txtUserCode.Attributes.Add("onclick", "MultiSearchVIP('../');");
            drpType.DataSource = new BLL.IntegralType().GetAllList();
            drpType.DataBind();
        }


        Session["FCode"] = "900500";

    }
    private void BindData(int pageIndex)
    {
        string start = txtStart.Text;
        string end="";
        if(txtEnd.Text.Length>0)
        {
            end=txtEnd.Text+" 23:59:59:999";
        }
        IntergalAccount ia = new BLL.ReportBL.IntergalAccount();
        DataSet ds = null;

        string ret=ia.GetIntergalAccount(txtUserCode.Text, drpType.SelectedIndex, start, end, out ds);
        if (ds!=null)
        {
            DataView dv = new DataView(ds.Tables[0], "UserCode is not null", "", DataViewRowState.CurrentRows);
            if (pageIndex < 0)
            {
                //导出EXcel文件
                DataTable2Excel(ds.Tables[0]);
            }
           // if (dv.Count > 0)
            {
                grdIntergral.PageIndex = pageIndex;
                grdIntergral.DataSource = dv;
                grdIntergral.DataBind();
            }

            if (dv.Count > 0)
            {
                dv = new DataView(ds.Tables[0], "UserCode is null", "", DataViewRowState.CurrentRows);
                grdIntergral.FooterRow.Cells[3].Text = dv[0]["IntegralTypeName"].ToString();
                grdIntergral.FooterRow.Cells[4].Text = dv[0]["JF"].ToString();
            }
        }
    }
    protected void DataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        e.Item.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
        e.Item.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); 
    }
 
    /// <summary>
    /// DataTable导出Excel
    /// </summary>
    /// <param name="dtData"></param>
    public   void DataTable2Excel(System.Data.DataTable dtData)
    {
        System.Web.UI.WebControls.DataGrid dgExport = null;
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";

            dtData.Rows.RemoveAt(0);
            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            dgExport = new System.Web.UI.WebControls.DataGrid();

            //dtData.Columns.Add("发放状态");
            ////dtData.Columns.Add("财务审核");
            ////dtData.Columns.Add("结算");


            dtData.Columns["UserCode"].ColumnName = "会员帐号";
            dtData.Columns["GKMC"].ColumnName = "会员名称";

            dtData.Columns["IntergalDateTime"].ColumnName = "积分时间";
            dtData.Columns["IntergalType"].ColumnName = "积分类型";

            dtData.Columns["IntegralTypeName"].ColumnName = "积分类型名称";
            dtData.Columns["JF"].ColumnName = "积分";
            //////dtData.Columns["Sprice"].ColumnName = "标准单价";
            //////dtData.Columns["Disc"].ColumnName = "折扣";
            //////dtData.Columns["Aprice"].ColumnName = "实际单价";
            //////dtData.Columns["Samt"].ColumnName = "标准金额";
            //////dtData.Columns["Aamt"].ColumnName = "实际金额";

            //for (int i = 0; i < dtData.Rows.Count; i++)
            //{
            //    //dtData.Rows[i]["发货/入库"] = dtData.Rows[i]["SendOrReceive"].ToString() == "1" ? "已发货" : "未发货"; 

            //    dtData.Rows[i]["发放状态"] = dtData.Rows[i]["SENDFLAG"].ToString() == "1" ? "已发放" : "未发放";

            //}
            //////移除列
            //dtData.Columns.Remove(dtData.Columns["SENDFLAG"]); 
            this.DataGrid.DataSource = dtData.DefaultView;
            this.DataGrid.AllowPaging = false;
            this.DataGrid.DataBind();
            this.DataGrid.RenderControl(htmlWriter);
            //dgExport.DataSource = dtData.DefaultView;
            //dgExport.AllowPaging = false;
            //dgExport.DataBind();

            //dgExport.RenderControl(htmlWriter);
            Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    } 
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData(0);
    }

    protected void btnExprot_Click(object sender, EventArgs e)
    {
        BindData(-1); //导出excel
    }
    

}
